//
//  XLPictureView.swift
//  XLVeibo
//
//  Created by macbook on 16/6/30.
//  Copyright © 2016年 buWangChuXing. All rights reserved.
//

import UIKit

//cell的可重用标识符
private let pictureViewCellId = "pictureViewCellId"

//cell 之间的距离
private let itemMargin: CGFloat = 5

 /// cell的宽和高
private let cellWH = (KScreenW - OriginalMargin * 2 - itemMargin * 2) / 3




class XLPictureView: UICollectionView {

    
    //声明一个供外界使用的配图属性
    var pic_urls:[XLPictureInfo]?{
      
        didSet{
            
            //
          let size = getPictureViewSize(pic_urls?.count ?? 0)
            
            //给其设置约束
            self.snp_updateConstraints { (make) in
                
                make.size.equalTo(size)
            }
            
            //拿到数组局第一时间刷新UI
            reloadData()
        }
    }
    
    
    //通过多少张配图来计算配图的大小
    func getPictureViewSize(count:Int) ->CGSize{
    
        //计算行数和列数
       /// 列数
        let col = count == 4 ? 2 : (count >= 3 ? 3 : count)
        ///行数
        let row = count == 4 ? 2 : ((count - 1) / 3 + 1)
        
        //计算配图的高
        let w = CGFloat(col) * cellWH + CGFloat(col - 1) * itemMargin
        let h = CGFloat(row) * cellWH + CGFloat(row - 1) * itemMargin
        
        return CGSize(width: w, height: h)
        
        
    }
    
       

    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置配图
    func setupUI(){
     
        backgroundColor = UIColor.clearColor()
        
        //设置数据源
        dataSource = self
        
       //注册cell
        registerClass(XLPictureViewCell.self, forCellWithReuseIdentifier: pictureViewCellId)
        //设置layout
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        //设置items
        layout.itemSize = CGSize(width: cellWH , height: cellWH)
        //设置最小间距
        layout.minimumLineSpacing = itemMargin
        layout.minimumInteritemSpacing = itemMargin
        
        //添加控件
        
        //约束自己
         self.snp_makeConstraints { (make) in
            
           make.center.equalTo(self)
        }
        
    }
    
}

extension XLPictureView:UICollectionViewDataSource{

    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pic_urls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureViewCellId, forIndexPath: indexPath) as!XLPictureViewCell

        
        cell.picinfo = pic_urls![indexPath.row]
        return cell
    }
 
}

//自定义cell
class XLPictureViewCell:UICollectionViewCell{

    //外界赋值
    var picinfo:XLPictureInfo?{
    
        didSet{
        
            //设置图片
            bgImage.sd_setImageWithURL(NSURL(string: picinfo?.thumbnail_pic ?? ""), placeholderImage: UIImage(named: "timeline_card_top_background"))
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //设置视图
    private func setupUI(){
    
       
        contentView.addSubview(bgImage)
        
        //添加约束
        bgImage.snp_makeConstraints { (make) in
            
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
    }
    
    
    //懒加载背景图
    private lazy var bgImage:UIImageView = {
    
        let image = UIImageView(imageName: "timeline_card_top_background")
        
        //指定图片的填充模式
        image.contentMode = .ScaleAspectFill
        image.clipsToBounds = true
        return image
    }()
    
    
    
}










